$(document).ready(function () {
    var payment = $("#000").attr("name");
    $("[name='pay_style']").click(function () {
        payment = $(this).next().attr("name");
    });
    $("[class='goods_list_td clearfix']").empty();
    var obj = [{"id":1,"img": "images/goods/goods012.jpg", "name": "商品 1", "price": 20, "quantity": 3, "amount": 60},
        {"id":2,"img": "images/goods/goods012.jpg", "name": "商品 2", "price": 25, "quantity": 4, "amount": 100}];
    var sum = 0;
    var count = 0;
    for (var i = 0; i < obj.length; i++) {
        $("<ul class=\"goods_list_td clearfix\">\n" +
            "        <li class=\"col01\">" + (i + 1) + "</li>\n" +
            "        <li class=\"col02\"><img src=\"" + obj[i].img + "\"></li>\n" +
            "        <li class=\"col03\" value='" + obj[i].name + "'>" + obj[i].name + "</li>\n" +
            "        <li class=\"col04\">500g</li>\n" +
            "        <li class=\"col05\">" + obj[i].price + "元</li>\n" +
            "        <li class=\"col06\">" + obj[i].quantity + "\n" +
            "        <li class=\"col07\">" + obj[i].amount + "元</li>\n" +
            "    </ul>").appendTo("#listt");
        sum += obj[i].amount;
        count++;
    }
    $("#sum").text("" + sum + "元");
    $("#count").text(count);
    $("#sum1").text("" + (sum + 10) + "元");
    var uid = 1;
    var detailedAddress = "这块";
    //alert("到函数前面了");
    $("#order_btn").click(function () {
        //alert("点击了");
        //添加订单
        $.get("order/addOrder", {
            "detailedAddress": detailedAddress,
            "status": "未支付",
            "amount": sum,
            "payment": payment,
            "uid": uid
        }, function (b) {
            //alert("到这了");
            if (b) {
                //添加订单项
                for (var i = 0; i < obj.length; i++) {
                    var id = obj[i].id;
                    var quantity = obj[i].quantity;
                    var price = obj[i].amount;
                    $.get("orderitem/addOrderItems", {"fid": id, "quantity": quantity, "price": price}, function (bo) {
                        if (bo) {
                            //删除购物车
                            $("order/removeShoppingTrolley", {"id": obj[i].id}, function (boo) {
                                if (boo&&i===obj.length-1) {
                                    localStorage.setItem('order_finish', 2);
                                    $('.popup_con').fadeIn('fast', function () {
                                        setTimeout(function () {
                                            $('.popup_con').fadeOut('fast', function () {
                                                window.location.href = 'list.html';
                                            });
                                        }, 3000)
                                    });
                                } else {
                                    alert("失败了！！");
                                }
                            });
                        } else {
                            alert("失败了！！")
                        }
                    });
                }
            } else {
                alert("失败了！！");
            }
        });
    });
});